package com.mdhw.common.base;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.domain.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: amoyiki
 * @date: 2022/3/19
 */
@Data
public class PageData<T> {

    @ApiModelProperty(value = "数据")
    private List<T> data;

    @ApiModelProperty(value = "页号")
    private Integer pageNo;

    @ApiModelProperty(value = "单页容量")
    private Integer pageSize;

    @ApiModelProperty(value = "总条数")
    private Integer totalCount;

    @ApiModelProperty(value = "总页数")
    private Integer totalPage;

    private Long timestamp = System.currentTimeMillis();

    public PageData(List<T> data, Integer pageNo, Integer pageSize, Integer totalCount, Integer totalPage) {
        this.data = data;
        this.pageNo = pageNo+1;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.totalPage = totalPage;
    }

    public PageData() {
    }

    public PageData(Page<T> page) {
        if (page == null) {
            return;
        }
        this.data = page.getContent();
        this.pageNo = page.getNumber();
        this.pageSize = page.getSize();
        this.totalPage = page.getTotalPages();
        this.totalCount = Integer.parseInt(page.getTotalElements()+"");
    }

    public static PageData plusPageToPageData(com.baomidou.mybatisplus.extension.plugins.pagination.Page page){
        Long current = page.getCurrent();
        Long size = page.getSize();
        List records = page.getRecords();
        Integer total = Integer.parseInt(page.getTotal()+"");
        Long totalPage =  total % size == 0? total/size : (total/size)+1;
        PageData pageData = new PageData();
        pageData.setData(records);
        pageData.setPageNo(Integer.parseInt(current.toString()));
        pageData.setPageSize(Integer.parseInt(size.toString()));
        pageData.setTotalPage(Integer.parseInt(totalPage.toString()));
        pageData.setTotalCount(total);
        return pageData;
    }

    public static PageData voPageToPageData(List data, com.baomidou.mybatisplus.extension.plugins.pagination.Page page){
        Long current = page.getCurrent();
        Long size = page.getSize();
        List records = page.getRecords();
        Integer total = Integer.parseInt(page.getTotal()+"");
        Long totalPage =  total % size == 0? total/size : (total/size)+1;
        PageData pageData = new PageData();
        pageData.setData(data);
        pageData.setPageNo(Integer.parseInt(current.toString()));
        pageData.setPageSize(Integer.parseInt(size.toString()));
        pageData.setTotalPage(Integer.parseInt(totalPage.toString()));
        pageData.setTotalCount(total);
        return pageData;
    }


    public static PageData emptyPageData(){
        Long current = 1L;
        Long size = 20L;
        List records = new ArrayList();
        Integer total = 0;
        Long totalPage = 1L;
        PageData pageData = new PageData();
        pageData.setData(records);
        pageData.setPageNo(Integer.parseInt(current.toString()));
        pageData.setPageSize(Integer.parseInt(size.toString()));
        pageData.setTotalPage(Integer.parseInt(totalPage.toString()));
        pageData.setTotalCount(total);
        return pageData;
    }
}
